Trò chơi Angry Birds trong UNITY Engine
31.684 lượt xem;
- RotationTweenProperty.cs
- specificTypes /
- properties /
- GoKit /
- Plugins /
- Assets /
- project /
1 using UnityEngine;
2 using System.Collections;
3
4
5 public class RotationTweenProperty : AbstractVector3TweenProperty
6 {
7 private bool _useLocalRotation;
8 public bool useLocalRotation { get { return _useLocalRotation; } }
9
10
11 public RotationTweenProperty( Vector3 endValue, bool isRelative = false, bool useLocalRotation = false ) : base( endValue, isRelative )
12 {
13 _useLocalRotation = useLocalRotation;
14 }
15
16
17 #region Object overrides
18
19 public override int GetHashCode()
20 {
21 return base.GetHashCode();
22 }
23
24
25 public override bool Equals( object obj )
26 {
27 // start with a base check and then compare if we are both using local values
28 if( base.Equals( obj ) )
29 return this._useLocalRotation == ((RotationTweenProperty)obj)._useLocalRotation;
30
31 // if we get here, we need to see if the other object is a eulerAngles tween of the same kind
32 var otherAsEuler = obj as EulerAnglesTweenProperty;
33 if( otherAsEuler != null )
34 return this._useLocalRotation == otherAsEuler.useLocalEulers;
35
36 return false;
37 }
38
39 #endregion
40
41
42 public override void prepareForUse()
43 {
44 _target = _ownerTween.target as Transform;
45
46 _endValue = _originalEndValue;
47
48 // if this is a from tween we need to swap the start and end values
49 if( _ownerTween.isFrom )
50 {
51 _startValue = _endValue;
52
53 if( _useLocalRotation )
54 _endValue = _target.localRotation.eulerAngles;
55 else
56 _endValue = _target.rotation.eulerAngles;
57 }
58 else
59 {
60 if( _useLocalRotation )
61 _startValue = _target.localRotation.eulerAngles;
62 else
63 _startValue = _target.rotation.eulerAngles;
64 }
65
66 // handle rotation carefully: when not relative, we always want to go the shortest possible distance to the new angle
67 if( _isRelative && !_ownerTween.isFrom )
68 _diffValue = _startValue + _endValue;
69 else
70 _diffValue = new Vector3( Mathf.DeltaAngle( _startValue.x, _endValue.x ), Mathf.DeltaAngle( _startValue.y, _endValue.y ), Mathf.DeltaAngle( _startValue.z, _endValue.z ) );
71 }
72
73
74 public override void tick( float totalElapsedTime )
75 {
76 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
77 var vec = GoTweenUtils.unclampedVector3Lerp( _startValue, _diffValue, easedTime );
78
79 if( _useLocalRotation )
80 _target.localRotation = Quaternion.Euler( vec );
81 else
82 _target.rotation = Quaternion.Euler( vec );
83 }
84
85 }
2 using System.Collections;
3
4
5 public class RotationTweenProperty : AbstractVector3TweenProperty
6 {
7 private bool _useLocalRotation;
8 public bool useLocalRotation { get { return _useLocalRotation; } }
9
10
11 public RotationTweenProperty( Vector3 endValue, bool isRelative = false, bool useLocalRotation = false ) : base( endValue, isRelative )
12 {
13 _useLocalRotation = useLocalRotation;
14 }
15
16
17 #region Object overrides
18
19 public override int GetHashCode()
20 {
21 return base.GetHashCode();
22 }
23
24
25 public override bool Equals( object obj )
26 {
27 // start with a base check and then compare if we are both using local values
28 if( base.Equals( obj ) )
29 return this._useLocalRotation == ((RotationTweenProperty)obj)._useLocalRotation;
30
31 // if we get here, we need to see if the other object is a eulerAngles tween of the same kind
32 var otherAsEuler = obj as EulerAnglesTweenProperty;
33 if( otherAsEuler != null )
34 return this._useLocalRotation == otherAsEuler.useLocalEulers;
35
36 return false;
37 }
38
39 #endregion
40
41
42 public override void prepareForUse()
43 {
44 _target = _ownerTween.target as Transform;
45
46 _endValue = _originalEndValue;
47
48 // if this is a from tween we need to swap the start and end values
49 if( _ownerTween.isFrom )
50 {
51 _startValue = _endValue;
52
53 if( _useLocalRotation )
54 _endValue = _target.localRotation.eulerAngles;
55 else
56 _endValue = _target.rotation.eulerAngles;
57 }
58 else
59 {
60 if( _useLocalRotation )
61 _startValue = _target.localRotation.eulerAngles;
62 else
63 _startValue = _target.rotation.eulerAngles;
64 }
65
66 // handle rotation carefully: when not relative, we always want to go the shortest possible distance to the new angle
67 if( _isRelative && !_ownerTween.isFrom )
68 _diffValue = _startValue + _endValue;
69 else
70 _diffValue = new Vector3( Mathf.DeltaAngle( _startValue.x, _endValue.x ), Mathf.DeltaAngle( _startValue.y, _endValue.y ), Mathf.DeltaAngle( _startValue.z, _endValue.z ) );
71 }
72
73
74 public override void tick( float totalElapsedTime )
75 {
76 var easedTime = _easeFunction( totalElapsedTime, 0, 1, _ownerTween.duration );
77 var vec = GoTweenUtils.unclampedVector3Lerp( _startValue, _diffValue, easedTime );
78
79 if( _useLocalRotation )
80 _target.localRotation = Quaternion.Euler( vec );
81 else
82 _target.rotation = Quaternion.Euler( vec );
83 }
84
85 }